﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CBMS.Interfaces;
using CBMS.Common;
using CBMS.Objects;
using CBMS.Business;
using CBMS.View;
using CBMS.Resources;

namespace CBMS.Uc
{
    public partial class ucMemberList : BaseControl
    {
        #region Fields
        private string _sortField = "first_name";
        private string _sortExp = "asc";
        private int _memberId = 0;
        private Boolean _IsAllowSort = true;
        private static IMemberRepository memResp { get; set; } 
        #endregion

        #region Init, Load
        public ucMemberList()
        {
            InitializeComponent();
            pagerTop.PageSize = CommonFunction.GetDefaultPageSize();
            memResp = new MemberRepository();
        }
        #endregion

        #region Methods
        private void SearchData()
        { 
            int toTal = 0;
            MemberCondition cond = new MemberCondition();
            if (!string.IsNullOrEmpty(txtShipNumber.Text.Trim()))
            {
                cond.membership_num = txtShipNumber.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtFirstName.Text.Trim()))
            {
                cond.first_name = txtFirstName.Text.Trim();
            }

            if (!string.IsNullOrEmpty(txtLastName.Text.Trim()))
            {
                cond.last_name = txtLastName.Text.Trim();
            }

            if (!string.IsNullOrEmpty(txtPhone.Text.Trim()))
            {
                cond.phone = txtPhone.Text.Trim();
            }

            if (chkVip.Checked)
            {
                cond.member_type = VALUES.DefautlValue.Code01;
            }
            IList<MemberResultDTO> lstObject = memResp.SearchMembersBySomeCondition(cond, pagerTop.PageIndex, pagerTop.PageSize, _sortField, _sortExp, ref toTal);
            try
            {
                dgvMembers.Rows.Clear();
                foreach (MemberResultDTO ps in lstObject)
                {
                    string strBirth = string.Empty;
                    if (ps.birthday != null)
                        strBirth = ps.birthday.Value.ToString(CommonFunction.FORMAT_DATE);
                    dgvMembers.Rows.Add(ps.first_name, ps.last_name, ps.membership_num, ps.subcategory_name, ps.email, ps.phone, strBirth, ps.address, ps.member_id);
                }
            }
            catch
            {
            }
            pagerTop.TotalRecord = toTal;
            pagerTop.SelectRecord = lstObject.Count;
            pagerTop.SetPaging();
            if (pagerTop.TotalPage > 1)
                _IsAllowSort = true;
            else
                _IsAllowSort = false;
        }

        private int GetMemberItemSelected()
        {
            int result = int.MinValue;
            try
            {
                result = int.Parse(dgvMembers.CurrentRow.Cells["member_id"].Value.ToString());
            }
            catch
            { }
            return result;
        }
        #endregion

        #region Handle Events
        private void ucMemberList_Load(object sender, EventArgs e)
        {
            memResp = new MemberRepository();
            SearchData();
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            pagerTop.PageIndex = 1;
            SearchData();
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            _sortField = "first_name";
            _sortExp = "ASC";
            pagerTop.PageIndex = 1;
            pagerTop.PageSize = CommonFunction.GetDefaultPageSize();
            SearchData();
        }
        private void pagerTop_PageIndexChanging(object obj, CustomControls.PageIndexChangeEventArgs e)
        {
            pagerTop.PageIndex = e.newPageIndex;
            SearchData();
        }
        private void pagerTop_PageSizeChanging(object obj, CustomControls.PageSizeChangeEventArgs e)
        {
            pagerTop.PageIndex = 1;
            pagerTop.PageSize = e.newPageSize;
            SearchData();
        }
        private void dgvMembers_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (!_IsAllowSort)
                return;
            string newSortField = dgvMembers.Columns[e.ColumnIndex].DataPropertyName;
            if (newSortField == _sortField)
            {
                if (_sortExp == "ASC")
                    _sortExp = "DESC";
                else _sortExp = "ASC";
            }
            else
            {
                _sortField = newSortField;
                _sortExp = "ASC";
            }
            SearchData();
        }
        private void dgvMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string columName = dgvMembers.Columns[e.ColumnIndex].Name;
                string value = dgvMembers.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                string id = dgvMembers.Rows[e.RowIndex].Cells["colId"].Value.ToString();
                if (columName == "colActive")
                { 
                    SearchData();
                }
            }
            catch
            {
            }
        }
        private void tsmiEdit_Click(object sender, EventArgs e)
        {
            frmMemberDetail f = new frmMemberDetail(this._memberId);
            f.StartPosition = FormStartPosition.CenterParent; 
            Common.CommonFunction.ShowForm(f, this);
            if (f.DialogResult == DialogResult.OK)
            {
                this.SearchData();
            } 
        }
        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            try
            {
              this._memberId  =  GetMemberItemSelected();
              if (this._memberId > 0)
              {
                  var objMember = memResp.GetMemberById(this._memberId);
                  DialogResult dialog = new DialogResult();
                  dialog = MessageBox.Show(string.Format(ErrorMessages.I0002, "[" + objMember.first_name + " " + objMember.last_name + "]"), ErrorMessages.T005, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                  if (dialog == DialogResult.Yes)
                  {
                      memResp.Delete(objMember);
                      memResp.SaveChanges();
                      this.SearchData();
                  }              
              }
            }
            catch { }

        }
        private void tsmiNew_Click(object sender, EventArgs e)
        {
            frmMemberDetail f = new frmMemberDetail();
            f.StartPosition = FormStartPosition.CenterParent; 
            Common.CommonFunction.ShowForm(f, this);
            if (f.DialogResult == DialogResult.OK)
            {
                this.SearchData();
            }
        }
        private void cmsEmployee_Opening(object sender, CancelEventArgs e)
        {
            this._memberId = GetMemberItemSelected();
            if (this._memberId > 0)
            {
                tsmiNew.Visible = true;
                tsmiEdit.Visible = true;
                tsmiDelete.Visible = true;
                tss3.Visible = true;
                tss4.Visible = true;
                tss1.Visible = true;
                tss2.Visible = true;
            }
            else
            {
                tsmiNew.Visible = true;
                tsmiEdit.Visible = false;
                tsmiDelete.Visible = false;
                tss3.Visible = false;
                tss4.Visible = false;
                tss1.Visible = true;
                tss2.Visible = true;
            }
        }
        #endregion  
    }
}
